package com.cloudcc.mobile.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.weight.ui.UserLogoDrawable;
import com.cloudcc.mobile.weight.ui.UserLogoDrawableRect;
import com.litesuits.common.utils.RandomUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * @author wangzhiqiang
 * @Description: 用户信息操作工具类
 * @date 2015-12-16 下午9:37:17
 */
public class UserUtils {
    private static int[] colors;

    /**
     * 获取自定义的头像颜色
     *
     * @return
     */
    public static int getRandomUserLogoColor(Context context) {
        int[] allLogoColors = getAllLogoColors(context);
        int random = RandomUtil.getRandom(allLogoColors.length);
        return allLogoColors[random];
    }

    /**
     * 获取 所有 头像的颜色值
     *
     * @param context
     * @return
     */
    public static int[] getAllLogoColors(Context context) {
        if (colors == null || colors.length == 0) {
            String[] array = context.getResources().getStringArray(
                    R.array.logo_colors);
            colors = new int[array.length];
            for (int i = 0; i < array.length; i++) {
                colors[i] = Color.parseColor(array[i]);
            }
        }
        return colors;
    }

    private DisplayImageOptions createDisPlayImageOptions(Drawable defult) {
        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnFail(defult)
                .displayer(new RoundedBitmapDisplayer(100))
                .resetViewBeforeLoading(false)
                .bitmapConfig(Bitmap.Config.RGB_565).delayBeforeLoading(1000)
                .cacheInMemory(true).imageScaleType(ImageScaleType.EXACTLY)// default
                .cacheOnDisc(true).build();
        return displayImageOptions;
    }

    /**
     * 设置用户头像
     *
     * @param url
     * @param imageView
     * @param backgroundColor
     * @param name
     * @param context
     */
    public static void setLogoRound(String url, ImageView imageView, int backgroundColor, String name, Context context) {
        Drawable defaultDrawable = getDefaultDrawable(backgroundColor, name);
        setLogo(url, imageView, defaultDrawable);
    }
    /**
     *默认头像 图片
     */
    public static void setLogoRoundMoren(String url, ImageView imageView,  Context context) {

        setLogoTouxiang(url, imageView);
    }

    public static void setLogoRound_z(String url, ImageView imageView,
                                      int backgroundColor, String name, Context context) {
        Drawable defaultDrawable = context.getResources().getDrawable(R.drawable.touxiangdefault);
        setLogo(url, imageView, defaultDrawable);
    }

    public static void setLogoRect(String url, ImageView imageView,
                                   int backgroundColor, String name, Context context) {
        Drawable defaultDrawable = getRectDrawable(backgroundColor, name);
        imageView.setImageDrawable(defaultDrawable);
        ImageLoader.getInstance().displayImage(url, imageView,
                DisplayerHelper.getNorImage(defaultDrawable));
    }

    private static void setLogo(String url, ImageView imageView, Drawable defaultDrawable) {
        imageView.setImageDrawable(defaultDrawable);
        DisplayImageOptions userLogoDisplayImageOptions = getUserLogoDisplayImageOptions(defaultDrawable);
        ImageLoader.getInstance().displayImage(url, imageView, userLogoDisplayImageOptions);
    }
    /**
    * 默认头像
    */
    private static void setLogoTouxiang(String url, ImageView imageView) {

        imageView.setImageResource(R.drawable.default_head);
        ImageLoader.getInstance().displayImage(url, imageView);
    }
    /**
     * 用户头像
     *
     * @param defult
     * @return
     */
    public static DisplayImageOptions getUserLogoDisplayImageOptions(
            Drawable defult) {
        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnFail(defult)
                .displayer(new RoundedBitmapDisplayer(300))
                .resetViewBeforeLoading(false)
                .bitmapConfig(Bitmap.Config.RGB_565).delayBeforeLoading(1000)
                .cacheInMemory(true).imageScaleType(ImageScaleType.EXACTLY)// default
                .cacheOnDisc(true).build();
        return displayImageOptions;
    }

    /**
     * 获得默认的头像 设置上 图片了
     *
     * @param backgrounColor
     * @param text
     * @return
     */
    public static Drawable getDefaultDrawable(int backgrounColor, String text) {
        UserLogoDrawable drawable = new UserLogoDrawable(backgrounColor,
                Color.WHITE, text);
        return drawable;
    }

    /**
     * 获得方形的图像
     *
     * @param backgrounColor
     * @param text
     * @return
     */
    public static Drawable getRectDrawable(int backgrounColor, String text) {
        UserLogoDrawableRect drawable = new UserLogoDrawableRect(
                backgrounColor, Color.WHITE, text);
        return drawable;
    }

    /**
     * 获取用户的 简称 名称
     *
     * @return
     */
    public static String getNickRowName(String name) {
        if (TextUtils.isEmpty(name)) {
            return "同事";
        }
        if (name.length() <= 2) {
            return name;
        } else {
            return name.substring(name.length() - 2, name.length());
        }
    }

    /**
     * 根据userId的hash 来唯一性的获得 用户 的 唯一随机颜色保证了 同一个用户仅仅有一种 颜色
     *
     * @param userId  用户id sting 如果用户id为空 就设置
     * @param context 上下文
     * @return 返回颜色值
     */
    public static int getBackGroudColorByUserIdHash(String userId,
                                                    Context context) {
        if (TextUtils.isEmpty(userId)) {
            return Color.WHITE;
        }
        int hashCode = userId.hashCode();
        int[] allLogoColors = getAllLogoColors(context);
        int colorIndex = Math.abs(hashCode % allLogoColors.length);
        return allLogoColors[colorIndex];
    }

    /**
     * 是否是当前正在登录的帐号
     *
     * @param userId id
     * @return
     */
    public static boolean isMe(String userId) {
        if (TextUtils.isEmpty(userId)) {
            return false;
        }
        if (StringUtils.equalsIgnoreCase(
                UserManager.getManager().getUser().userId, userId))
            return true;
        return false;
    }

}
